/**
 * Created by Administrator on 2018/1/18.
 */

layui.use(['jquery', 'laydate','form','layer','laypage','table'], function(){
    var $ = layui.jquery,
        laydate = layui.laydate,
        layer = layui.layer,
        table = layui.table,
        form = layui.form;

    laydate.render({
        elem: '#start'//指定元素
    });

    laydate.render({
        elem: '#end' //指定元素

    });

    var under_config = {
        elem: '#region',
        url:"/user/get_region_list",
        method:'post',
       // cellMinWidth:100,

        cols:  [[
            //标题栏
            {field: 'uid', title: '用户ID',align: 'center',width:'80'},
            {field: 'name', title: '代理姓名',align: 'center',width:'80'},
            {field: 'firm_id', title: '交易商编号',align: 'center',width:'120'},
            {field: 'region', title: '代理区域',align: 'center',width:'100'},
            {field: 'card_code', title: '区域编号（身份证前6位）',align: 'center',width:'600'},
            {field: 'status', title: '状态',align: 'center',width:'100'},
            {field: 'addtime', title: '添加时间',align: 'center',width:'180'},
            {field: 'updatetime', title: '更新时间',align: 'center',width:'180'},
            {field: 'remark', title: '备注',align: 'center',width:'120'},
            {fixed: 'right', align:'center', title: '操作',toolbar: '#barDemo',width:'120'}
        ]],
        //data:data.data,
        page: true,
        limit: 15,
        height: 680,
        // size:'sm',
        done: function(res, curr, count){
            layer.closeAll();

        }
    };


    //添加代理区域
    
   $('#add').on('click',function () {       //添加


       layer.open({
           type:2
           ,area:['1000px', '500px']
           ,resize:false
           ,btn:['确认', '取消']
           ,shade:'0.5'
           ,title:"添加代理区域"
           ,content: '/user/add_proxy_region'
           ,success:function(layero, index){
               bodys = layer.getChildFrame('body', index);
               // $form = body.find('#set-form');
               $region = bodys.find('input[name=region]');
               $uid = bodys.find('select[name=uid]');
               $remark = bodys.find('input[name=remark]');
               $card_code = bodys.find('input[name=card_code]');

               //console.log($input.val());


           }
           ,yes:function () {

               console.log(bodys);
               $region_val = $region.val();
               $uid_val = $uid.val();
               $remark_val = $remark.val();
               $status_val = bodys.find('input:radio[name=status]:checked').val();
               $card_code_val = $card_code.val();

               if($region_val==''){
                   layer.alert('请填写代理区域');
                   return;
               }
               if($uid_val==''){
                   layer.alert('请选择代理');
                   return;
               }
               if($card_code_val==''){
                   layer.alert('请填写区域编号');
                   return;
               }
               if($status_val=='' || $status_val==undefined){
                   layer.alert('请选择状态');
                   return;
               }

               $.post('/user/add_proxy_region?id=',{region:$region_val,uid:$uid_val,remark:$remark_val,card_code:$card_code_val,status:$status_val},function (result) {
                   if(result.code==200)
                   {
                       layer.alert(result.msg,function () {
                           //window.location.href='/user/brokerageset?first=1&proxyid='+proxyid;
                           window.location.reload();
                       })
                   }else{
                       layer.alert(result.msg);
                   }
               },'json');
           }
       });
       form.render();

   });
    //监听表格工具条
    table.on('tool(region)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        var tr = obj.tr; //获得当前行 tr 的DOM对象

        if(layEvent === 'edit'){ //编辑
            var id = data.id;

            layer.open({
                type:2
                ,area:['1000px', '500px']
                ,resize:false
                ,btn:['确认', '取消']
                ,shade:'0.5'
                ,title:"代理区域设置"
                ,content: '/user/edit_proxy_region?id='+id
                ,success:function(layero, index){
                    body = layer.getChildFrame('body', index);
                   // $form = body.find('#set-form');
                    $region = body.find('input[name=region]');
                    $uid = body.find('select[name=uid]');
                    $remark = body.find('input[name=remark]');
                    $card_code = body.find('input[name=card_code]');

                    //console.log($input.val());


                }
                ,yes:function () {


                    $region_val = $region.val();
                    $uid_val = $uid.val();
                    $remark_val = $remark.val();
                    $status_val = body.find('input:radio[name=status]:checked').val();
                    $card_code_val = $card_code.val();

                    if($region_val==''){
                        layer.alert('请填写代理区域');
                        return;
                    }
                    if($uid_val==''){
                        layer.alert('请选择代理');
                        return;
                    }
                    if($card_code_val==''){
                        layer.alert('请填写区域编号');
                        return;
                    }
                    if($status_val=='' || $status_val==undefined){
                        layer.alert('请选择状态');
                        return;
                    }

                    $.post('/user/edit_proxy_region?id='+id,{region:$region_val,uid:$uid_val,remark:$remark_val,card_code:$card_code_val,status:$status_val},function (result) {
                        if(result.code==200)
                        {
                            layer.alert(result.msg,function () {
                                //window.location.href='/user/brokerageset?first=1&proxyid='+proxyid;
                                window.location.reload();
                            })
                        }else{
                            layer.alert(result.msg);
                        }
                    },'json');
                }
            });
            form.render();

        }
    });



    //方法级渲
    var ins1 = table.render(under_config);

    //表格搜索和重载
    var $ = layui.$,
        active = {
            reload: function(){
                var index = layer.msg('数据加载中,请耐心等待...', {
                    icon: 16,shade: 0.5,time:15000
                });

                var keyword = $('#keyword').val();

                ins1.reload({
                    where: {
                        //根据条件搜索

                        keyword:keyword

                    }
                });
            },

        };

    $('.content-search .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

});
